package cn.com.xinjinfu.pdoulcProduct.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.adapter.ReturnedMoneyListAdapter;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.ReturnedMoneyBean;
import cn.com.xinjinfu.pdoulcProduct.entity.ReturnedMoneyBean.ReceiveList;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.xlistview.XListView;

/**
 * 歷史回款计划列表
 * Created by android-adt on 2016/4/7.
 */
public class ReturnedMoneyHistoryAct extends BaseActivity implements XListView.IXListViewListener {

    private User user;
    private XListView mListView;
    private Handler mHandler;
    private RequestQueue requen;
    private ReturnedMoneyListAdapter mAdapter;
    private ReturnedMoneyBean returnedMoneyBean;
    private ArrayList<ReceiveList> receiveList;
    private LinearLayout no_invest_view;
    private int currPage;
    private List<ReceiveList> myList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_returned_money);
        TitleManager.showTitle(this, "回款历史", "");
        user = AccountManager.getInstance().getUser();
        requen = Volley.newRequestQueue(this);
        currPage = 1;
        myList = new ArrayList<>();
        no_invest_view = (LinearLayout) findViewById(R.id.no_invest_view);
        mListView = (XListView) findViewById(R.id.xListView);
        mListView.setPullLoadEnable(true);// 是否显示下拉刷新和上啦加载
        mListView.setPullRefreshEnable(false);
        mAdapter = new ReturnedMoneyListAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setXListViewListener(this);
        mHandler = new Handler();
        loginRequest(currPage);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position>0) {
                    ReceiveList receiveList = myList.get(position - 1);
                    String month = receiveList.getReceive_month();
                    Intent intent = new Intent(ReturnedMoneyHistoryAct.this, ReturnedCalendarAct.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("month", month);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });

    }

    /**
     * 网络请求
     */
    private void loginRequest(int currPage) {
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("1417");
            parameters.put("id", user.getCurrUser().getSign());
            parameters.put("currPage", currPage + "");
            parameters.put("pageSize", "9");
            parameters.put("type", "0"); //请求类型（默认0） 0-已收 其他-待收
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, null);
            requen.add(request);
        }
    }

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i(jobj.toString());
            returnedMoneyBean = JSON.parseObject(jobj.toString(), ReturnedMoneyBean.class);
            receiveList = returnedMoneyBean.getReceiveList();
            if (receiveList != null) {
                if (currPage == 1) {
                    if (receiveList.isEmpty()) {
                        no_invest_view.setVisibility(View.VISIBLE);
                    } else {
                        no_invest_view.setVisibility(View.GONE);
                        mAdapter.setList(receiveList);
                        myList.clear();
                        myList.addAll(receiveList);
                    }
                    onLoad();
                } else {
                    if (receiveList==null||receiveList.isEmpty()){
                        ToastManager.showShort(ReturnedMoneyHistoryAct.this,"没有更多数据！");
                    }
                    mAdapter.addList(receiveList);
                    myList.addAll(receiveList);
                    onLoad();
                }
            } else {
                no_invest_view.setVisibility(View.VISIBLE);
            }

        }
    };

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                currPage++;
                loginRequest(currPage);
            }
        }, 1500);
    }
}
